{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "ezIfTzzGjfkv"
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from RMKMC import *"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "nIqYdAi_jfk1"
   },
   "source": [
    "Implement the measurements 'Accuracy' and 'Purity'. NMI is already provided in sklearn."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "APGxaXTfjfk6"
   },
   "outputs": [],
   "source": [
    "from sklearn.metrics import confusion_matrix\n",
    "from sklearn.utils.linear_assignment_ import linear_assignment\n",
    "from sklearn.metrics.cluster import normalized_mutual_info_score\n",
    "\n",
    "# Accuracy (inspired by https://smorbieu.gitlab.io/accuracy-from-classification-to-clustering-evaluation/)\n",
    "def accu(y_true, y_pred):\n",
    "    def _make_cost_m(cm):\n",
    "        s = np.max(cm)\n",
    "        return (- cm + s)\n",
    "    cm = confusion_matrix(y_true, y_pred)\n",
    "    indexes = linear_assignment(_make_cost_m(cm))\n",
    "    js = [e[1] for e in sorted(indexes, key=lambda x: x[0])]\n",
    "    cm2 = cm[:, js]\n",
    "    print(cm2)\n",
    "    return np.trace(cm2)/np.sum(cm)\n",
    "\n",
    "# Purity \n",
    "def purity(y_true, y_pred):\n",
    "    cm = confusion_matrix(y_true, y_pred)\n",
    "    return np.sum(np.max(cm, axis = 0)) / np.sum(cm)\n",
    "\n",
    "# Utility function to output the measurement results\n",
    "def my_output(data_name, gamma, y_true, y_pred):\n",
    "    accuracy = accu(y_true,y_pred)\n",
    "    print(\"Accuracy of RMKMC on\", data_name, \"with gamma=\", gamma, \"is\", accuracy)\n",
    "\n",
    "    puri = purity(y_true,y_pred)\n",
    "    print(\"Purity of RMKMC on\", data_name, \"with gamma=\", gamma, \"is\", puri)\n",
    "\n",
    "    nmi = normalized_mutual_info_score(y_true, y_pred)\n",
    "    print(\"NMI of RMKMC on\", data_name, \"with gamma=\", gamma, \"is\", nmi)\n",
    "    return (accuracy, puri, nmi)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "unySROJHjflX"
   },
   "source": [
    "## Handwritten Numerals"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "1EBawRtAjflY"
   },
   "source": [
    "Read raw datasets. The 6 views are already separated. There are exactly 200 samples in each class."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "jlGuo8A9jflY"
   },
   "outputs": [],
   "source": [
    "fou = pd.read_csv('mfeat-fou', sep=' +', header=None)\n",
    "fac = pd.read_csv('mfeat-fac', sep=' +', header=None)\n",
    "kar = pd.read_csv('mfeat-kar', sep=' +', header=None)\n",
    "pix = pd.read_csv('mfeat-pix', sep=' +', header=None)\n",
    "zer = pd.read_csv('mfeat-zer', sep=' +', header=None)\n",
    "mor = pd.read_csv('mfeat-mor', sep=' +', header=None)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "mC5GPrrWjfla"
   },
   "source": [
    "Create the true labels."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "i_jJlLi8jflb"
   },
   "outputs": [],
   "source": [
    "labels = [0]*200 + [1]*200 + [2]*200 + [3]*200 + [4]*200 + [5]*200 + [6]*200 + [7]*200 + [8]*200 + [9]*200\n",
    "\n",
    "Xs = [fou.values, fac.values, kar.values, pix.values, zer.values, mor.values]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "_16l4AY0jfld"
   },
   "source": [
    "Run the algorithm using random initialization."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "d4yjxpuzjfld"
   },
   "outputs": [],
   "source": [
    "k = 10\n",
    "gamma = 3\n",
    "n_iterations = 300\n",
    "\n",
    "ind_matrix, Fs, aa = RMKMC(Xs, k, gamma, n_iter = n_iterations)\n",
    "print(\"weight distribution is\", aa)\n",
    "my_labels = deonehot(ind_matrix)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "Mr_bu6qIjflg"
   },
   "source": [
    "Report performance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "E0UMvbmrjflg"
   },
   "outputs": [],
   "source": [
    "my_output(\"digits\", gamma, labels, my_labels)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "5k8AvG1gjfli"
   },
   "source": [
    "Run the algorithm using kmeans++ intialization."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "ow4WxnF3jflj"
   },
   "outputs": [],
   "source": [
    "k = 10\n",
    "gamma = 3\n",
    "n_iterations = 300\n",
    "\n",
    "ind_matrix, Fs, aa = RMKMC(Xs, k, gamma, n_iter = n_iterations, initialization = 'pp')\n",
    "print(\"weight distribution is\", aa)\n",
    "my_labels = deonehot(ind_matrix)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "EB09aGsTjfll"
   },
   "source": [
    "Report performance."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "5b_62N7sjfll"
   },
   "outputs": [],
   "source": [
    "my_output(\"digits\", gamma, labels, my_labels)"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "colab": {
   "name": "testing.ipynb",
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
